package com.darren.euc.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonFormat;

@Entity
@Table(name = "euc_roles")
public class Role implements Serializable {

	private static final long serialVersionUID = 2861478466077717714L;

	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;
	
	@Column(name = "role_name")
	private String roleName;	
	
	@Column(name = "role_type")
	private String roleType;

	@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
	@Column(name = "create_time")
	private Date createTime;

	@ManyToMany(cascade = CascadeType.ALL,targetEntity=Function.class,fetch = FetchType.EAGER)
	@JoinTable(name= "euc_role_function", 
	joinColumns = {@JoinColumn(name = "role_id", nullable = false, updatable = false) },
	inverseJoinColumns = { @JoinColumn(name = "function_id", nullable =false, updatable = false) })	
	@OrderBy("id asc")  
	private Set<Function> functions = new HashSet<Function>();
	
	@ElementCollection(targetClass=Integer.class)//,fetch=FetchType.EAGER)//,fetch = FetchType.EAGER) 
    // 映射保存集合属性的表  
    @CollectionTable(name="euc_role_admin", // 指定表名为euc_role_admin  
        joinColumns=@JoinColumn(name="role_id" , nullable=false))  
    // 指定保存集合元素的列为 function_id  
    @Column(name="admin")  
    // 映射集合元素索引的列  
    //@OrderColumn(name="role_id")
    private List<Integer> admins  = new ArrayList<Integer>();

	@Column(name = "function_names")
	private String functionNames;
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String getRoleType() {
		return roleType;
	}

	public void setRoleType(String roleType) {
		this.roleType = roleType;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getFunctionNames() {
		functionNames = "";
		if(functions != null && functions.size() > 0 ){			
			List<Function> firstLevel = functions.stream().filter(function -> function.getParentId().equals(0)).collect(Collectors.toList());
			Iterator<Function> firstIterator = firstLevel.iterator();			
			while(firstIterator.hasNext()){
				Function fun = firstIterator.next();
				functionNames += " " + fun.getFunctionName(); 
				List<Function> secondLevel = functions.stream().filter(function -> function.getParentId().equals(fun.getFunctionId())).collect(Collectors.toList());
				Iterator<Function> secondIterator = secondLevel.iterator();	
				while(secondIterator.hasNext()){
					Function secondfunction = secondIterator.next();
					functionNames += " " + secondfunction.getFunctionName(); 
				}
			}
		}
		return functionNames;
	}


	public void setFunctionNames(String functionNames) {
		this.functionNames = functionNames;
	}

	public Set<Function> getFunctions() {
		return functions;
	}

	public void setFunctions(Set<Function> functions) {
		this.functions = functions;
	}

	public List<Integer> getAdmins() {
		return admins;
	}

	public void setAdmins(List<Integer> admins) {
		this.admins = admins;
	}	
}
